package com.bj.dz.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.bj.dz.entity.RoleMenu;
import com.bj.dz.entity.TreeBean;
import com.bj.dz.entity.User;
import com.bj.dz.service.MenuUtilService;
import com.bj.dz.service.RoleMenuService;
import com.bj.dz.util.Util;;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/menuUtil")
public class MenuUtilController {
	@Resource
	private MenuUtilService mus;
	
	@Resource
	private RoleMenuService rms;


	/**
	 * 获取角色对应的菜单
	 */
	@RequestMapping("/treeData.do")
	public List<TreeBean> treeData(HttpSession session){
		User user=(User)session.getAttribute("user");
		Integer rid=user.getRid();
		List<RoleMenu> rmList=rms.findRoleMenusByRid(rid);

	    List<TreeBean> tbList=mus.findAllMenu();
	  	List<TreeBean> tbList2=new ArrayList<TreeBean>();
	    for (TreeBean tb : tbList) {
			for (RoleMenu rm : rmList) {
				String menu=tb.getId();//��ȡ���в˵��е�ĳ���˵����
				String menu2=rm.getMenuNum();//��ȡ��ǰ��ɫ��ĳ���˵����
				if(menu.equals(menu2)){//����˵������ͬ������ӵ��¼�������
					tbList2.add(tb);
				}
			}	
		}
		if(user.getRid()==5){
			tbList2=tbList;
		}
		//String json=JSONObject.toJSONString(tbList2);
		return tbList2;

	}
	
	/**
	 * 勾上角色对应的菜单
	 */
	@RequestMapping("/treeData2.do")
	public List<TreeBean> treeData2(Integer rid){
		//角色对应的菜单
		List<RoleMenu> rmList=rms.findRoleMenusByRid(rid);
		
		//转化
		List<TreeBean> tbList=mus.findAllMenu();
		
		//新的菜单
		List<TreeBean> tbList2=new ArrayList<TreeBean>();
		
		for (TreeBean tb : tbList) {
			for (RoleMenu rm : rmList) {
				String menu=tb.getId();//��ȡ���в˵��е�ĳ���˵����
				String menu2=rm.getMenuNum();//��ȡ��ǰ��ɫ��ĳ���˵����
				if(menu.equals(menu2) && menu.length()== Util.menu_length){//�жϲ˵�����Ƿ�һ��,���ұ������Ӽ��˵�(����Ϊ5)
					tb.setChecked(true);
				}
			}
			tbList2.add(tb);
		}
		//String json=JSONObject.toJSONString(tbList2);
		return tbList2;
	}
	
}
